require "Object"
require "LingDang"
require "GeZi"

lingdangCount_max = 20

--LingDang class
LingDangFactory = Object:new{
	world = nil,
	lingdangs = nil,
	lingdangFile = "lingdang.cc2d",
	geziFile = "gezi/gezi.cc2d",
	perHeight = 20,
	moveSpeed = 40,
	
	collideCount = 0,
}

function LingDangFactory:onEnter()
	Misc:log("LingDangFactory : onEnter .. " .. Misc:getNodeName(self))
	self.perHeight = g_winSize.height*4/lingdangCount_max
	self.collideCount = 0
end

function LingDangFactory:tick(dt)
	for i,v in pairs(self.lingdangs) do
		v:tick(dt)
	end
	
	self:product()
	
	local curPos = self:getPositionLua()
	curPos.y = curPos.y - self.moveSpeed*dt
	self:setPosition(curPos.x, curPos.y)
end

function LingDangFactory:clear()
	self.collideCount = 0
	lingdangCount = 0
	for i,v in pairs(self.lingdangs) do
		if v then
			v:removeFromParentAndCleanup(true)
		end
	end
	self.lingdangs = nil
	self.lingdangs = {}
	self:setPosition(0, 0)
end

function LingDangFactory:product()
	if not self.world then
		return
	end
	
	local curCount = tableCount(self.lingdangs)
	
	if curCount == 0 then
		local lingdang = LingDang:create(self.lingdangFile, self)
		self.lingdangs[lingdang.lingdangID] = lingdang
		local pos = CCPoint((g_winSize.width-64)*math.random(0,1000)/1000.0+32, g_winSize.height+64)
		lingdang:setPosition(pos.x, pos.y)
	elseif curCount < lingdangCount_max then
		local pos = CCPoint((g_winSize.width-64)*math.random(0,1000)/1000.0+32, 
								self.lingdangs[lingdangCount]:getPositionLua().y+self.perHeight)
		local lingdang = nil
		Misc:log("ling dang count============ == "..curCount)
		if lingdangCount%20 == 0 then
			lingdang = GeZi:create(self.geziFile, self)
			if pos.x > g_winSize.width*0.5 then
				lingdang.velocity.x = -lingdang.velocity.x
				lingdang:setFlipX(true)
			end
		else
			lingdang = LingDang:create(self.lingdangFile, self)
		end
		self.lingdangs[lingdang.lingdangID] = lingdang
		lingdang:setPosition(pos.x, pos.y)
	end
end

function LingDangFactory:onExit()
	Misc:log("LingDangFactory : onExit .. " .. Misc:getNodeName(self))
end

function LingDangFactory:checkTuzi(tuzi)
	local tuziPos = tuzi:getParent():convertToWorldSpace(tuzi:getPositionLua())
	for i,v in pairs(self.lingdangs) do
		if not v.bDispeared and Misc:collideNode(v, tuzi:getCollideNode()) then
			tuzi:tiaoYue(tuziPos)
			self.collideCount = self.collideCount +1
			v:showScore(self.collideCount*10)
			v:collide()
			break
		end
	end
end

function LingDangFactory:create(world, lingdangFactory)
	local newFactory = lingdangFactory or LingDangFactory:new()
	
	local factoryNode = CCNode:create()
	
	Misc:setNodeName(factoryNode, "LingDangFactory")
	tolua.setpeer(factoryNode, newFactory)
	
	world:addChild(factoryNode)
		
	local function onEnterOrExit(eventType)
		if eventType == kCCNodeOnExit then
			factoryNode:onExit()
		else
			factoryNode:onEnter()
		end
	end
	
	factoryNode:registerScriptHandler(onEnterOrExit)
	
	if factoryNode:isRunning() then
		factoryNode:onEnter()
	end
	
	factoryNode.world = world
	factoryNode.lingdangs = {}
	
	return factoryNode
end